<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>拖拽删除</title>
  <style>
    .dustbin {
      background-color: #646363;
      width: 150px;
      height: 300px;
      text-align: center;
      color: rgb(165, 161, 161);
      font-size: 40px;
      float: left;
    }

    .dragbox {
      float: left;
      padding: 0px 20px;
      width: 500px;
    }

    .draglist {
      background-color: #eeebeb;
      border: 1px dashed rgb(128, 126, 126);
      margin-bottom: 10px;
      height: 50px;
      line-height: 50px;
      text-indent: 2em;
    }

    .dragremind {
      padding-top: 20px;
      clear: both;
    }
  </style>
</head>

<body>
  <div class="dustbin">
    <br>垃 <br> 圾 <br> 箱
  </div>
  <div class="dragbox">
    <div class="draglist" title="拖拽我" draggable="true">列表1</div>
    <div class="draglist" title="拖拽我" draggable="true">列表2</div>
    <div class="draglist" title="拖拽我" draggable="true">列表3</div>
    <div class="draglist" title="拖拽我" draggable="true">列表4</div>
    <div class="draglist" title="拖拽我" draggable="true">列表5</div>
    <div class="draglist" title="拖拽我" draggable="true">列表6</div>
  </div>
  <div class="dragremind"></div>
</body>

</html>
<script>
  let draglists = document.querySelectorAll(".draglist");
  let dustbin = document.querySelector(".dustbin");
  let currentDragDom = null;

  for (let i = 0; i < draglists.length; i++) {
    draglists[i].ondragstart = function (ev) {
      ev.dataTransfer.setData("text", ev.target.innerHTML);
      // 自定义拖拽图标
      let imgDom = new Image('../images/1.jpg');
      ev.dataTransfer.setDragImage(imgDom, 0, 0);
      currentDragDom = this;
    }

    draglists[i].ondragend = function (ev) {
      // 清除数据
      ev.dataTransfer.clearData("text");
      currentDragDom = null;
    }
  }

  dustbin.ondragenter = function (ev) {
    this.style.color = "#fff";
  }

  dustbin.ondragover = function (ev) {
    ev.preventDefault();
  }

  dustbin.ondrop = function (ev) {
    if (currentDragDom) {
      let info = ev.dataTransfer.getData("text");
      document.querySelector(".dragremind").innerHTML = info + "被丢到了垃圾箱"
      currentDragDom.remove();
    }
  }







</script>